package com.sugon.util;

import com.sugon.Application;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

/**
 * @description:
 * @author: jichenglu
 * @create: 2019-09-03 14:24
 **/
public class FfmpegThread extends Thread {

    private String threadName;

    private String rtspUrl;

    private String rtmpUrl;

    private String ffmpegPath;

    private String display;

    private long playTime;

    public FfmpegThread(String threadName,String rtspUrl, String rtmpUrl, String ffmpegPath, String display, long playTime){
        this.threadName = threadName;
        this.rtspUrl = rtspUrl;
        this.rtmpUrl = rtmpUrl;
        this.ffmpegPath = ffmpegPath;
        this.display = display;
        this.playTime=playTime;
    }

    @Override
    public void run() {
        try {
            transfer();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //ffmpeg.exe -rtsp_transport tcp -i rtsp://admin:nzcsy123@123.161.103.67:555/main_stream -f flv -r 25 -s 1920x1080 -an rtmp://10.10.204.82:1935/live/1
    //ffmpeg -re -rtsp_transport tcp -i "rtsp://120.194.111.67:9090/dss/monitor/param?cameraid=1002501%240&substream=1" -f flv
    // -vcodec libx264 -vprofile baseline -acodec aac -ar 44100 -strict -2 -ac 1 -f flv -s 1280x720 -q 10 "rtmp://172.21.52.17:1985/myapp/testv"
    public void transfer() throws Exception{

        rtmpUrl = rtmpUrl + threadName;
        List<String> command = new ArrayList<>();
        //command.add("sudo");
        command.add("ffmpeg");
        command.add("-rtsp_transport");
        command.add("tcp");
        command.add("-re");
        command.add("-i");
        command.add(rtspUrl);
        command.add("-f");
        command.add("flv");
        command.add("-vcodec");
        command.add("libx264");
        command.add("-vprofile");
        command.add("baseline");
        command.add("-acodec");
        command.add("acc");
        command.add("-ar");
        command.add("44100");
        command.add("-strict");
        command.add("-2");
        command.add("-ac");
        command.add("1");
        command.add("-f");
        command.add("flv");
        command.add("-s");
        command.add(display);
        command.add("-an");
        command.add("-q");
        command.add("10");
        command.add(rtmpUrl);
        /*command.add(ffmpegPath);
        command.add("-rtsp_transport");
        command.add("tcp");
        command.add("-re");
        command.add("-i");
        command.add(rtspUrl);
        command.add("-f");
        command.add("flv");
        command.add("-r");
        command.add("25");
        command.add("-s");
        command.add(display);
        command.add("-an");
        command.add(rtmpUrl);*/

        //System.out.println("正在执行 ffmpeg 指令");

        ProcessBuilder builder = new ProcessBuilder(command);
        Process process = builder.start();

        //System.out.println("ffmpeg 指令执行结束");

        Application.processMap.put(threadName,process);
        InputStream errorStream = process.getErrorStream();
        InputStreamReader isr = new InputStreamReader(errorStream);
        BufferedReader br = new BufferedReader(isr);
        String line = "";
        new Thread(
                ()->{
                    try{
                        Thread.sleep(1000*playTime);
                    }catch (Exception e){
                        process.destroy();
                        return;
                    }
                    Application.urlMap.remove(rtspUrl);
                    Application.processMap.remove(threadName);
                    process.destroy();
                }
        ).start();
        while ((line = br.readLine()) != null) {
            System.out.println(line);
        }
        if (br != null) {
            br.close();
        }
        if (isr != null) {
            isr.close();
        }
        if (errorStream != null) {
            errorStream.close();
        }

    }

}
